package com.bridgeintelligent.tag.bulkload.service.es.query.condition;


import com.bridgeintelligent.tag.bulkload.api.BulkLoadException;
import com.bridgeintelligent.tag.bulkload.api.FilterTypeConfig;
import com.bridgeintelligent.tag.bulkload.api.fileload.Filter;
import com.wayneleo.quickstart.framework.ExceptionCode;
import lombok.Getter;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

/**
 * ----------->>>>枚举策略类----------->>>>
 * Created by JackCheung on 2019/10/14 4:59 PM.
 */
@Getter
public enum ConditionStrategy {
    /**
     * 等于
     */
    EQUAL(FilterTypeConfig.EQUAL) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.termQuery(filter);
        }
    },
    /**
     * 不等于
     */
    NO_EQUAL(FilterTypeConfig.NO_EQUAL) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.termQuery(filter));
        }
    },
    /**
     * 大于
     */
    GREATER(FilterTypeConfig.GREATER) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, false);
            return ConditionHelper.gtQuery(filter);
        }
    },
    /**
     * 大于(日期)
     */
    GREATER_DATE(FilterTypeConfig.GREATER_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.gtQueryDate(filter);
        }
    },
    /**
     * 小于
     */
    LESS(FilterTypeConfig.LESS) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, false);
            return ConditionHelper.ltQuery(filter);
        }
    },
    /**
     * 小于(日期)
     */
    LESS_DATE(FilterTypeConfig.LESS_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.ltQueryDate(filter);
        }
    },
    /**
     * 大于等于
     */
    GREATER_EQUAL(FilterTypeConfig.GREATER_EQUAL) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, false);
            return ConditionHelper.gteQuery(filter);
        }
    },
    /**
     * 大于等于(日期)
     */
    GREATER_EQUAL_DATE(FilterTypeConfig.GREATER_EQUAL_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.gteQueryDate(filter);
        }
    },
    /**
     * 小于等于
     */
    LESS_EQUAL(FilterTypeConfig.LESS_EQUAL) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, false);
            return ConditionHelper.lteQuery(filter);
        }
    },
    /**
     * 小于等于(日期)
     */
    LESS_EQUAL_DATE(FilterTypeConfig.LESS_EQUAL_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.lteQueryDate(filter);
        }
    },
    /**
     * 在……之中
     */
    AMONG(FilterTypeConfig.AMONG) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, true);
            return ConditionHelper.amongQuery(filter);
        }
    },
    /**
     * 在……之中(日期)
     */
    AMONG_DATE(FilterTypeConfig.AMONG_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormatString(filter);
            return ConditionHelper.amongQueryDate(filter);
        }
    },
    /**
     * 不在……之中
     */
    NOT_AMONG(FilterTypeConfig.NOT_AMONG) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter, true);
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.amongQuery(filter));
        }
    },
    /**
     * 不在……之中(日期)
     */
    NOT_AMONG_DATE(FilterTypeConfig.NOT_AMONG_DATE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormatString(filter);
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.amongQueryDate(filter));
        }
    },
    /**
     * 精确包含
     */
    CONTAIN(FilterTypeConfig.CONTAIN) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.termQuery(filter);
        }
    },
    /**
     * 包含
     */
    CONTAIN_EVERYTHING(FilterTypeConfig.CONTAIN_EVERYTHING) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.termQuery(filter);
        }
    },
    /**
     * 精确不包含
     */
    NOT_CONTAIN(FilterTypeConfig.NOT_CONTAIN) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.termQuery(filter));
        }
    },
    /**
     * 模糊包含
     */
    CONTAIN_VAGUE(FilterTypeConfig.CONTAIN_VAGUE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter);
            return ConditionHelper.containVagueQuery(filter);
        }
    },
    /**
     * 模糊不包含
     */
    NOT_CONTAIN_VAGUE(FilterTypeConfig.NOT_CONTAIN_VAGUE) {
        @Override
        public QueryBuilder exec(Filter filter) {
            ConditionHelper.checkFormat(filter);
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.containVagueQuery(filter));
        }
    },
    /**
     * 存在(字符型)
     */
    TERM(FilterTypeConfig.TERM) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.notExistQuery(filter));
        }
    },
    /**
     * 不存在(字符型)
     */
    NO_TERM(FilterTypeConfig.NO_TERM) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.notExistQuery(filter);
        }
    },
    /**
     * 存在(数值型)
     */
    TERM_NUM(FilterTypeConfig.TERM_NUM) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return QueryBuilders.boolQuery()
                                .mustNot(ConditionHelper.notExistNumQuery(filter));
        }
    },
    /**
     * 不存在(数值型)
     */
    NO_TERM_NUM(FilterTypeConfig.NO_TERM_NUM) {
        @Override
        public QueryBuilder exec(Filter filter) {
            return ConditionHelper.notExistNumQuery(filter);
        }
    };

    public abstract QueryBuilder exec(Filter filter);

    private String value;

    ConditionStrategy(String value) {
        this.value = value;
    }

    /**
     * 根据filter类型将filter转成对应QueryBuilder
     *
     * @param filter Filter
     * @return QueryBuilder
     */
    public static QueryBuilder create(Filter filter) {
        for (ConditionStrategy esStrategyEnum : values()) {
            if (esStrategyEnum.value.equals(filter.getType())) {
                return esStrategyEnum.exec(filter);
            }
        }
        throw new BulkLoadException(new ExceptionCode(999, "查询逻辑未定义！"));
    }
}
